package com.zhaokuner.ctrl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestWord {
	// 生成Word2007版本
	private FileInputStream fis2007;
	private XWPFDocument doc2007;
	private XWPFWordExtractor word2007;

	// 生成Word2003版本
	private FileInputStream fis2003;
	private HWPFDocument doc2003;
	private WordExtractor word2003;

	// 创建Word输出流
	private FileOutputStream fos;

	@Before
	public void setUp() throws Exception {

		// 初始化2007版本
		fis2007 = new FileInputStream(new File("D:/test.docx"));
		doc2007 = new XWPFDocument(fis2007);
		word2007 = new XWPFWordExtractor(doc2007);

		// 初始化2003版本
		fis2003 = new FileInputStream(new File("D:/test2.doc"));
		doc2003 = new HWPFDocument(fis2003);
		word2003 = new WordExtractor(doc2003);

		// 初始化输出流
		fos = new FileOutputStream(new File("D:/testCreateWord.docx"));
	}

	@Test
	public void testReadWord2003() {
		// 直接通过getText()获取文本
		String text = word2003.getText();
		// 获取总页数
		doc2003.getSummaryInformation().getPageCount();
		// 获取总字数
		doc2003.getSummaryInformation().getWordCount();
		Assert.assertNotNull(text);
	}

	@Test
	public void testReadWord2007() {
		// 直接通过getText()获取文本
		String text = word2007.getText();

		// 获取总页数
		doc2007.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();

		// 获取去除空格的总页数
		doc2007.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters();

		// 获取带空格的总页数
		doc2007.getProperties().getExtendedProperties().getUnderlyingProperties().getCharactersWithSpaces();

		Assert.assertNotNull(text);
	}

	/*
	 * 演示如何创建Word文档
	 */
	@Test
	public void testWriteWord2007() throws IOException {
		XWPFDocument doc = new XWPFDocument();

		// 创建段落
		XWPFParagraph p1 = doc.createParagraph();
		// 设置样式,此时样式为一个正方形包围文字
		p1.setAlignment(ParagraphAlignment.CENTER);
		p1.setBorderBottom(Borders.DOUBLE);
		p1.setBorderTop(Borders.DOUBLE);
		p1.setBorderRight(Borders.DOUBLE);
		p1.setBorderLeft(Borders.DOUBLE);
		p1.setBorderBetween(Borders.SINGLE);
		p1.setVerticalAlignment(TextAlignment.TOP);

		// 创建1段文字,通过段落创建
		XWPFRun r1 = p1.createRun();
		// 设置是否粗体
		r1.setBold(true);
		r1.setText("The quick brown fox");
		r1.setBold(true);
		r1.setFontFamily("Courier");
		r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
		r1.setTextPosition(100);

		XWPFParagraph p2 = doc.createParagraph();
		p2.setAlignment(ParagraphAlignment.RIGHT);

		p2.setBorderBottom(Borders.DOUBLE);
		p2.setBorderTop(Borders.DOUBLE);
		p2.setBorderRight(Borders.DOUBLE);
		p2.setBorderLeft(Borders.DOUBLE);
		p2.setBorderBetween(Borders.SINGLE);

		XWPFRun r2 = p2.createRun();
		r2.setText("Hello Hello Hello Hello Hello Hello Hello");
		r2.setStrike(true);
		r2.setFontSize(20);

		XWPFRun r3 = p2.createRun();
		r3.setText("World World World World World World World");
		r3.setStrike(true);
		r3.setFontSize(20);

		XWPFParagraph p3 = doc.createParagraph();
		p3.setWordWrap(true);
		// 设置该段落填充满本页,下面在显示新文本将在下一页显示
		p3.setPageBreak(true);

		p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
		p3.setAlignment(ParagraphAlignment.BOTH);
		p3.setSpacingLineRule(LineSpacingRule.EXACT);

		p3.setIndentationFirstLine(600);

		doc.write(fos);
	}

	@After
	public void tearDown() throws IOException {
		if (fis2003 != null) {
			fis2003.close();
		}
		if (fis2007 != null) {
			fis2007.close();
		}
		if (fos != null) {
			fos.close();
		}
	}
}
